#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int left = 0, right = nums.size() - 1,k = nums.size() - 1;
        vector<int> result(nums.size(), 0);
        for(int i = left, j = right; i <= j;){
            if(pow(nums[i], 2) < pow(nums[j], 2)){
                result[k--] = pow(nums[j], 2);
                j--;
            }
            else{
                result[k--] = pow(nums[i], 2);
                i++;
            }
        }

        return result;
    }
};

int main()
{
    Solution s;
    vector<int> nums = {-4,-1,0,3,10};
    vector<int> result = s.sortedSquares(nums); 
    int n = result.size();
    for(int i = 0;i < n; i++){
        cout << result[i] << " ";
    }
    return 0;
}